package com.yf.exam.modules.repo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.exam.core.api.ApiRest;
import com.yf.exam.core.api.controller.BaseController;
import com.yf.exam.core.api.dto.BaseIdReqDTO;
import com.yf.exam.core.api.dto.BaseIdsReqDTO;
import com.yf.exam.core.api.dto.PagingReqDTO;
import com.yf.exam.modules.repo.dto.DepartTypeDTO;
import com.yf.exam.modules.repo.entity.DepartType;
import com.yf.exam.modules.repo.service.DepartTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* <p>
* 学科类型控制器
* </p>
*
* @author
*/
@Api(tags={"学科类型"})
@RestController
@RequestMapping("/exam/api/depart/type")
public class DepartTypeController extends BaseController {

    @Autowired
    private DepartTypeService baseService;

    /**
    * 添加或修改
    * @param reqDTO
    * @return
    */
    @RequiresRoles(value = {"sa", "teacher"}, logical = Logical.OR)
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiRest save(@RequestBody DepartTypeDTO reqDTO) {
        baseService.save(reqDTO);
        return super.success();
    }

    /**
    * 批量删除
    * @param reqDTO
    * @return
    */
    @RequiresRoles(value = {"sa", "teacher"}, logical = Logical.OR)
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiRest delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        baseService.removeByIds(reqDTO.getIds());
        return super.success();
    }

    /**
    * 查找详情
    * @param reqDTO
    * @return
    */
    @RequiresRoles(value = {"sa", "teacher"}, logical = Logical.OR)
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiRest<DepartTypeDTO> find(@RequestBody BaseIdReqDTO reqDTO) {
        DepartType entity = baseService.getById(reqDTO.getId());
        DepartTypeDTO dto = new DepartTypeDTO();
        BeanUtils.copyProperties(entity, dto);
        return super.success(dto);
    }

    /**
    * 分页查找
    * @param reqDTO
    * @return
    */
    @RequiresRoles(value = {"sa","teacher"},logical = Logical.OR)
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiRest<IPage<DepartTypeDTO>> paging(@RequestBody PagingReqDTO<DepartTypeDTO> reqDTO) {

        //分页查询并转换
        IPage<DepartTypeDTO> page = baseService.paging(reqDTO);

        return super.success(page);
    }

    /**
     * 查找所有学科类型
     * @return
     */
    @ApiOperation(value = "查找所有学科类型")
    @RequestMapping(value = "/list-all", method = { RequestMethod.POST})
    public ApiRest<List<DepartTypeDTO>> listAll() {
        List<DepartTypeDTO> list = baseService.listAll();
        return super.success(list);
    }
}